#include <iostream>

using namespace std;
const int N = 1e5 + 10;
using ll = long long;
ll p[N];

int main()
{

    int n;
    cin >> n;
    ll tval = 0;
    cin >> p[0];
    for (int i = 1; i < n; i++)
    {
        cin >> p[i];
        if (p[i] > p[i - 1]) tval += p[i] - p[i - 1];
    }

    cout << tval << endl;
    return 0;
}